package com.ruoyi.api.user;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.api.user.requestVo.LocationIdRequestVo;
import com.ruoyi.api.user.requestVo.LocationSetRequestVo;
import com.ruoyi.api.user.requestVo.MachineBindRequestVo;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.user.domain.Location;
import com.ruoyi.user.domain.LocationSet;
import com.ruoyi.user.service.ILocationService;
import com.ruoyi.user.service.ILocationSetService;

import io.swagger.annotations.ApiOperation;

/**
 * 问题反馈 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-12-23
 */
@Controller
@RequestMapping("/api/location")
public class ApiLocationController extends BaseController
{
    private String prefix = "location";
	
	@Autowired
	private ILocationService locationService;
	
	@Autowired
	private ILocationSetService locationSetService;
	
	/**
	 * 获取运动轨迹
	 */
	@PostMapping("/history")
	@ResponseBody
	@ApiOperation(value="获取运动轨迹", notes="获取运动轨迹",produces = "application/json")
	public HashMap<String, Object> history(@RequestBody MachineBindRequestVo requestVo)
	{		
		Location param = new Location();
		param.setImei(requestVo.getCode());
		List<Location> locationList = locationService.selectLocationList(param);
		return successApp(locationList);
	}
	
	/**
	 * 电子围栏设置
	 */
	@PostMapping("/roundSet")
	@ResponseBody
	@ApiOperation(value="电子围栏设置", notes="电子围栏设置",produces = "application/json")
	public HashMap<String, Object> roundSet(@RequestBody LocationSetRequestVo locationSetRequestVo)
	{		
		LocationSet one = new LocationSet();
		one.setCreateTime(new Date());
		one.setLat(locationSetRequestVo.getLat());
		one.setLon(locationSetRequestVo.getLon());
		one.setRadius(locationSetRequestVo.getRadius());
		one.setImei(locationSetRequestVo.getImei());
		one.setAddress(locationSetRequestVo.getAddress());
		locationSetService.insertLocationSet(one);
		return successApp("");
	}
	
	/**
	 * 电子围栏列表
	 */
	@PostMapping("/roundSetList")
	@ResponseBody
	@ApiOperation(value="电子围栏列表", notes="电子围栏列表",produces = "application/json")
	public HashMap<String, Object> roundSetList(@RequestBody MachineBindRequestVo requestVo)
	{		
		LocationSet one = new LocationSet();
		one.setImei(requestVo.getCode());
		List<LocationSet> list = locationSetService.selectLocationSetList(one);
		return successApp(list);
	}
	
	/**
	 * 电子围栏删除
	 */
	@PostMapping("/delete")
	@ResponseBody
	@ApiOperation(value="电子围栏删除", notes="电子围栏删除",produces = "application/json")
	public HashMap<String, Object> delete(@RequestBody LocationIdRequestVo locationIdRequestVo)
	{		
		locationSetService.deleteLocationSetByIds(locationIdRequestVo.getId());
		return successApp("");
	}
	
	/**
	 * 发起定位请求
	 */
	@PostMapping("/locationRequest")
	@ResponseBody
	@ApiOperation(value="发起定位请求", notes="发起定位请求",produces = "application/json")
	public HashMap<String, Object> locationRequest(@RequestBody MachineBindRequestVo requestVo)
	{		
		JSONObject param = new JSONObject();
		param.put("imei", requestVo.getCode());
		System.out.println(param.toJSONString());
		String response = HttpUtils.sendPutJson("http://api.jiai.pro:8080/jiai/location", param.toJSONString(),requestVo.getCode());
		System.out.println(response);
		JSONObject res = (JSONObject) JSONObject.parse(response);
		String errc = (String) res.get("code");
		if("0000".equals(errc)){
			System.out.println("执行获取定位请求方法成功！！！");
		}else{
			String message = (String) res.get("message");
			System.out.println("执行获取定位请求方法失败："+message);
		}
		return successApp("");
	}

}
